﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HanGu.Services.Interface;
using HanGu.Entity;
using System.Transactions;

namespace HanGu.Services
{
    public class Roles : BaseService, IRoles
    {
        public List<Role> GetAll()
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.Role.OrderBy(c => c.Name).ToList();
            }
        }

        public List<Role> GetAll(UniversallySearchParam searchparma)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                var query = obj.Role.Where(c => 1 == 1);
                if (!string.IsNullOrEmpty(searchparma.Ttile))
                {
                    query = query.Where(c => c.Name.IndexOf(searchparma.Ttile) >= 0);
                }
                query = base.Paging<Role, string>(c => c.Name, query, searchparma.Pagination);
                return query.ToList();
            }
        }

        public List<VRolePurview> GetVRolePurviewByID(int id)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.VRolePurview.Where(c => c.RoleID == id).OrderBy(c => c.Order).ToList();
            }

        }
        public Role GetByID(int id)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.Role.Where(c => c.ID == id).FirstOrDefault();
            }
        }
        public bool Add(Role model)
        {
            //using (TransactionScope scope = new TransactionScope())
            {
                using (HaiGuEntities obj = new HaiGuEntities())
                {
                    var id = model.ID;
                    if (id == 0)
                    {
                        obj.Role.AddObject(model);
                        obj.SaveChanges();
                        id = model.ID;
                    }
                    else
                    {
                        string sql = "delete from RolePurview where RoleID={0}";
                        obj.ExecuteStoreCommand(string.Format(sql, id));
                        base.Update<Role>(model);
                    }
                    foreach (var item in model.SelectedRoles.Split(','))
                    {
                        if (!string.IsNullOrEmpty(item))
                        {
                            var rolePurview = new RolePurview()
                            {
                                RoleID = id,
                                ModulelID = item
                            };
                            obj.RolePurview.AddObject(rolePurview);
                        }
                    }
                    obj.SaveChanges();
                    //scope.Complete();
                    return true;
                }
            }
        }
        public bool Update(VRolePurview model)
        {
            return false;
        }
        public bool Delete(int id)
        {
            //using (TransactionScope scope = new TransactionScope())
            {
                using (HaiGuEntities obj = new HaiGuEntities())
                {
                    string sql = "delete from Role where ID={0};delete from RolePurview where RoleID={0};";
                    obj.ExecuteStoreCommand(string.Format(sql, id));
                    //scope.Complete();
                    return true;
                }
            }
        }
    }
}
